package com.hey.lib.depth;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.sskj.depth.bean.DepthData;
import com.sskj.depth.bean.IDepthData;
import com.sskj.depth.view.DepthMapView;
import com.sskj.depth.view.DepthShaderView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView buyList;
    private RecyclerView sellList;
    private BaseAdapter<String> buyAdapter;
    private BaseAdapter<String> sellAdapter;
    private List<String> buyData, sellData;

    private DepthMapView depthView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        depthView = findViewById(R.id.depthView);
        buyList = findViewById(R.id.buyList);
        sellList = findViewById(R.id.sellList);
        buyList.setLayoutManager(new LinearLayoutManager(this));
        sellList.setLayoutManager(new LinearLayoutManager(this));
        buyData = new ArrayList<>();
        buyData.add("1");
        buyData.add("fa2");
        buyData.add("3gas");
        buyData.add("gg");
        buyData.add("asd5");
        buyData.add("6a");
        buyData.add("6a");
        buyData.add("6a");
        buyData.add("6a");
        buyData.add("6a");
        buyData.add("6a");
        buyData.add("6a");
        buyData.add("6a");
        buyData.add("6a");
        buyData.add("6a");
        buyData.add("6a");
        buyData.add("6a");
        buyData.add("6a");
        sellData = new ArrayList<>();
        sellData.add("1");
        sellData.add("12");
        sellData.add("3");
        sellData.add("1fa");
        sellData.add("1ga");
        sellData.add("1ga");
        sellData.add("1123");
        sellData.add("1312");
        sellData.add("1312");
        sellData.add("1312");
        sellData.add("1312");
        sellData.add("1312");
        sellData.add("1312");
        sellData.add("1312");
        sellData.add("1312");
        sellData.add("1312");
        sellData.add("1312");
        sellData.add("1312");
        buyAdapter = new BaseAdapter<String>(R.layout.item_depth, buyData, buyList) {
            @Override
            public void bind(BaseViewHolder holder, String item) {
                holder.setText(R.id.index, holder.getAdapterPosition() + "");
                DepthShaderView depthView = holder.getView(R.id.depth);
                depthView.setProgress((float) (Math.random() * 100));
                depthView.setDirection(DepthShaderView.RIGHT);
                depthView.setShadowColor(ContextCompat.getColor(MainActivity.this, R.color.colorAccent));
            }
        };
        sellAdapter = new BaseAdapter<String>(R.layout.item_depth, sellData, sellList) {
            @Override
            public void bind(BaseViewHolder holder, String item) {
                holder.setText(R.id.index, holder.getAdapterPosition() + "");
                DepthShaderView depthView = holder.getView(R.id.depth);
                depthView.setProgress((float) (Math.random() * 100));
                depthView.setDirection(DepthShaderView.LEFT);
                depthView.setShadowColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary));
            }
        };
        handler.postDelayed(runnable, 500);
        sellList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                if (recyclerView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) {
                    buyList.scrollBy(dx, dy);
                }
            }
        });
        buyList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                if (recyclerView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) {
                    sellList.scrollBy(dx, dy);
                }
            }
        });

        List<IDepthData> data = new ArrayList<>();
        for (int i = 0; i < 200; i++) {
            if (i == 0) {
                data.add(new DepthData( String.valueOf(Math.random() * 10000), String.valueOf((5-i)*100)));
            } else {
                data.add(new DepthData(String.valueOf(data.get(i - 1).getVolume() + (float) Math.random() * 10000), String.valueOf((5-i)*100)));

            }
        }

        List<IDepthData> sellData = new ArrayList<>();
        for (int i = 0; i < 200; i++) {
            if (i == 0) {
                sellData.add(new DepthData(String.valueOf((float) Math.random() * 10000), String.valueOf(i*100)));
            } else {
                sellData.add(new DepthData(String.valueOf(data.get(i - 1).getVolume() + (float) Math.random() * 10000), String.valueOf(i+100)));

            }}

        depthView.setDrawText(true);
        depthView.setData(data, sellData);
    }

    final Handler handler = new Handler();
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            buyAdapter.setNewData(buyData);
            sellAdapter.setNewData(sellData);
            handler.postDelayed(runnable, 500);
        }
    };


}
